package system.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import system.httpModel.Menu;
import system.httpModel.Resources;
import system.service.MenuService;
import system.service.ResourcesService;

import common.controller.BaseController;
import common.httpModel.EasyuiTreeNode;
import common.httpModel.Json;
import common.httpModel.SessionInfo;
import common.util.ResourceUtil;

@Controller
@RequestMapping("/menuController")
public class MenuController extends BaseController {
	private MenuService menuService;
	private ResourcesService resourcesService;
	
	public ResourcesService getResourcesService() {
		return resourcesService;
	}
	@Autowired
	public void setResourcesService(ResourcesService resourcesService) {
		this.resourcesService = resourcesService;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	@Autowired
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}


	/**
	 * 获取树菜单
	 */
	@RequestMapping(params="tree")
	@ResponseBody
	public List<EasyuiTreeNode> tree(HttpSession session,String id){
		SessionInfo sessionInfo=(SessionInfo) session.getAttribute(ResourceUtil.getSessionInfoName());
		
		return menuService.tree(id,sessionInfo.getUser());
	}
	/**
	 * 跳转到管理页面
	 */
	@RequestMapping(params="menu")
	public String menu(){
		return "/admin/menu";
	}
	
	/**
	 * 获取菜单treegrid
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "treegrid")
	@ResponseBody
	public List<Menu> treegrid(String id) {
		return menuService.treegrid(id);
	}
	
	/**
	 * 添加菜单
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping(params = "add")
	@ResponseBody
	public Json add(Menu menu) {
		Json j = new Json();
		Menu m = menuService.add(menu);
		
		Resources resources=new Resources();
		resources.setMenuId(m.getId());
		resources.setText(m.getText());
		resources.setSrc(m.getSrc());
		resources.setSeq(m.getSeq());
		
		resources=resourcesService.add(resources);
		
		j.setSuccess(true);
		j.setMsg("添加成功!");
		
		return j;
	}

	/**
	 * 编辑菜单
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping(params = "edit")
	@ResponseBody
	public Json edit(Menu menu) {
		Json j = new Json();
		Menu m = menuService.edit(menu);

		Resources resources=new Resources();
		resources.setMenuId(m.getId());
		resources.setText(m.getText());
		resources.setSrc(m.getSrc());
		resources.setSeq(m.getSeq());
		
		resources=resourcesService.edit(resources);
		
		if(resources!=null){
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		}

		return j;
	}

	/**
	 * 删除菜单
	 * @param menu
	 * @return
	 */
	@RequestMapping(params="del")
	@ResponseBody
	public Json del(Menu menu){
		Json j=new Json();
		boolean tf=	menuService.del(menu);
		if(tf){
			Resources resources=new Resources();
			resources.setMenuId(menu.getId());
			resourcesService.queryResources(menu.getId());
			
			resourcesService.del(resources);
			j.setMsg("删除成功,联动删除在资源管理器里面的菜单！");
			j.setSuccess(true);
		}else{
			j.setMsg("请选择子菜单一个一个删除！");
			j.setSuccess(false);
		}
		
		return j;
	}
}




